package com.zy.service.impl;

import com.zy.dao.DepartmentDao;
import com.zy.dao.impl.DepartmentDaoImpl;
import com.zy.pojo.Department;
import com.zy.service.DepartmentService;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xiaoqi
 * Date: 2021/5/31
 * Time: 下午 16:30
 * Description: 部门服务层接口实现类
 */
public class DepartmentServiceImpl implements DepartmentService {
    private DepartmentDao departmentDao = new DepartmentDaoImpl();

    @Override
    public Boolean insertDepartment(Department department) {
        department.setCreateTime(new Date());
        return departmentDao.insertDepartment(department) > 0;
    }

    @Override
    public Boolean deleteDepartmentById(Integer id) {
        // 首先查询该部门是否有员工
        Integer peopleCount = departmentDao.getDepartmentPeopleCount(id);
        if (peopleCount == 0) {
            return departmentDao.deleteDepartmentById(id) > 0;
        }
        return false;
    }

    @Override
    public Boolean updateDepartment(Department department) {
        return departmentDao.updateDepartment(department) > 0;
    }

    @Override
    public List<Department> getDepartmentList(Integer page, Integer limit) {
        if (page != null) {
            page = (page - 1) * limit;
        }
        return departmentDao.getDepartmentList(page, limit);
    }

    @Override
    public Department getDepartmentById(Integer id) {
        return departmentDao.getDepartmentById(id);
    }

    @Override
    public Integer getDepartmentCount() {

        return departmentDao.getDepartmentCount();
    }
}